
package com.jjwxc.jwjskandriod.readActivity.model;

import android.annotation.SuppressLint;

import com.jjwxc.jwjskandriod.config.Url;
import com.jjwxc.jwjskandriod.framework.http.FFNetWork;
import com.jjwxc.jwjskandriod.framework.http.FFNetWorkCallBack;
import com.jjwxc.jwjskandriod.readActivity.bean.BookChapterBean;
import com.jjwxc.jwjskandriod.readActivity.bean.ChapterInfoBean;
import com.jjwxc.jwjskandriod.readActivity.bean.CollBookBean;
import com.jjwxc.jwjskandriod.readActivity.bean.JListChapterBean;
import com.jjwxc.jwjskandriod.readActivity.bean.JchapterBean;
import com.jjwxc.jwjskandriod.readActivity.ui.IReaderBookModel;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Single;
import io.reactivex.SingleEmitter;
import io.reactivex.SingleOnSubscribe;

public class ContentSoDuModelImpl implements IReaderBookModel {

    public static ContentSoDuModelImpl getInstance() {
        return new ContentSoDuModelImpl();
    }


    @SuppressLint("CheckResult")
    public Single<List<BookChapterBean>> getBookChapters(CollBookBean collBookBean) {

        return Single.create(new SingleOnSubscribe<List<BookChapterBean>>() {
            @Override
            public void subscribe(SingleEmitter<List<BookChapterBean>> emitter) throws Exception {
                List<BookChapterBean> list = new ArrayList<>();
                new FFNetWork().post(Url.listChapter, null, new FFNetWorkCallBack<JListChapterBean>() {
                    @Override
                    public void onSuccess(JListChapterBean response) {
                        if (response.getCode() == 200) {
                            List<JListChapterBean.DataBean> data = response.getData();

                            for (int i = 0; i < data.size(); i++) {
                                BookChapterBean bookChapterBean = new BookChapterBean();
                                bookChapterBean.setBookId(collBookBean.get_id());
                                bookChapterBean.setTitle(data.get(i).getChapterName());
                                bookChapterBean.setId(data.get(i).getChapterId());
                                bookChapterBean.setPosition(i);
                                bookChapterBean.setUnreadble(false);
                                bookChapterBean.setVip(data.get(i).isIsVip());
                                bookChapterBean.setHasAuth(data.get(i).isHasAuth());
                                bookChapterBean.setMonthSubscribe(collBookBean.isMonthSubscribe());
                                bookChapterBean.setPrice(data.get(i).getUserPrice()+"");
                                bookChapterBean.setJuan(data.get(i).getJuan());
                                list.add(bookChapterBean);
                            }
                            emitter.onSuccess(list);
                        }

                    }

                    @Override
                    public boolean onFail(JListChapterBean response) {
                        return false;
                    }
                }, JListChapterBean.class, "{\"bookId\":\"" + collBookBean.get_id() + "\"}");

            }
        });

    }


    /////////////////////////////////////////////////////////////////////////////////


    @Override
    public Single<ChapterInfoBean> getChapterInfo(String bookId, String chapterId) {
        return Single.create(new SingleOnSubscribe<ChapterInfoBean>() {
            @Override
            public void subscribe(SingleEmitter<ChapterInfoBean> emitter) throws Exception {
                new FFNetWork().post(Url.getChapter, null, new FFNetWorkCallBack<JchapterBean>() {
                    @Override
                    public void onSuccess(JchapterBean response) {
                        ChapterInfoBean chapterInfoBean = new ChapterInfoBean();
                        if (response.getCode() == 200) {
                            JchapterBean.DataBean dataBean = response.getData();
                            chapterInfoBean.setBody(dataBean.getContent());
                            chapterInfoBean.setTitle(dataBean.getChapterName());
                            emitter.onSuccess(chapterInfoBean);
                        }
                    }

                    @Override
                    public boolean onFail(JchapterBean response) {
                        return false;
                    }
                }, JchapterBean.class, "{\"bookId\":\"" + bookId + "\",\"chapterId\":\"" + chapterId + "\"}");
            }
        });
    }
}
